package com.real8.romemp.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;

public class FeedViewImpl extends Composite implements FeedView {

	private static FeedViewImplUiBinder uiBinder = GWT
			.create(FeedViewImplUiBinder.class);

	interface FeedViewImplUiBinder extends UiBinder<Widget, FeedViewImpl> {
	}

	private Presenter presenter;

	@UiField
	HTMLPanel foodAvailablePanel;
	@UiField
	SpanElement localMerchantShipsSpan;
	@UiField
	SpanElement gainedFoodSpan;
	@UiField
	SpanElement foodReserveSpan;
	@UiField
	SpanElement availableFoodSpan;

	@UiField
	HTMLPanel enoughFoodPanel;

	@UiField
	HTMLPanel insufficientFoodPanel;
	@UiField
	SpanElement foodShortageSpan;

	@UiField
	HTMLPanel extraFoodCostPanel;
	@UiField
	SpanElement costSpan;

	@UiField
	Button continueButton;

	public FeedViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(Presenter aPresenter) {
		this.presenter = aPresenter;
	}

	@Override
	public void hideAll() {
		foodAvailablePanel.setVisible(false);
		enoughFoodPanel.setVisible(false);
		insufficientFoodPanel.setVisible(false);
		extraFoodCostPanel.setVisible(false);
		continueButton.setVisible(false);
	}

	@Override
	public void showFoodAvailable(int localMerchantShips, int gainedFood,
			int foodReserve, int availableFood) {
		localMerchantShipsSpan.setInnerText("" + localMerchantShips);
		gainedFoodSpan.setInnerText("" + gainedFood);
		foodReserveSpan.setInnerText("" + foodReserve);
		availableFoodSpan.setInnerText("" + availableFood);

		foodAvailablePanel.setVisible(true);
	}

	@Override
	public void showEnoughFood() {
		enoughFoodPanel.setVisible(true);
	}

	@Override
	public void showInsufficientFood(int foodShortage) {
		foodShortageSpan.setInnerText("" + foodShortage);

		insufficientFoodPanel.setVisible(true);
	}

	@Override
	public void showExtraFoodCost(int cost) {
		costSpan.setInnerText("" + cost);

		extraFoodCostPanel.setVisible(true);
	}

	@Override
	public void showContinue() {
		continueButton.setVisible(true);
	}

	@UiHandler("continueButton")
	void onContinueButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onContinue();
		}
	}
}
